package com.igoal.admin.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.ui.ModelMap;

import com.igoal.admin.web.model.AdminBaseModel;
import com.igoal.service.ActionLoggerService;
import com.igoal.service.ApplicationContextService;
import com.igoal.service.CategoryManager;
import com.igoal.service.CommonService;
import com.igoal.service.GoalEntryManager;
import com.igoal.service.GoalManager;
import com.igoal.service.UserAccountService;
import com.igoal.service.UserGoalManager;
import com.igoal.service.UserManagerExt;

public abstract class AdminBaseController {
	protected GoalManager goalManager;
	protected GoalEntryManager goalEntryManager;
	protected UserManagerExt userManagerExt;
	protected CommonService commonService;
	protected UserGoalManager userGoalManager;
	protected ApplicationContextService contextService;
	protected ActionLoggerService actionLoggerService;
	protected UserAccountService userAccountService;
	protected CategoryManager categoryManager;
	
	@Autowired
	public void setCategoryManager(CategoryManager categoryManager) {
		this.categoryManager = categoryManager;
	}

	@Autowired
	public void setGoalManager(GoalManager goalManager) {
		this.goalManager = goalManager;
	}

	@Autowired
	public void setGoalEntryManager(GoalEntryManager goalEntryManager) {
		this.goalEntryManager = goalEntryManager;
	}

	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}

	@Autowired
	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}

	@Autowired
	public void setUserGoalManager(UserGoalManager userGoalManager) {
		this.userGoalManager = userGoalManager;
	}

	@Autowired
	public void setContextService(ApplicationContextService contextService) {
		this.contextService = contextService;
	}

	@Autowired
	public void setActionLoggerService(ActionLoggerService actionLoggerService) {
		this.actionLoggerService = actionLoggerService;
	}

	@Autowired
	public void setUserAccountService(UserAccountService userAccountService) {
		this.userAccountService = userAccountService;
	}

	public String getViewPrefix() {
		return "admin/";
	}	

	public abstract void initRightPage(AdminBaseModel baseModel, HttpServletRequest request);

	public abstract void initLeftPage(AdminBaseModel baseModel, HttpServletRequest request);
	
	public String getUserInSession() {
		return SecurityContextHolder.getContext().getAuthentication().getName();
	}
	
	/**
	 * Initialize username and apply model by data keyword
	 * @param modelMap
	 * @param model
	 */
	public void applyModel(ModelMap modelMap, AdminBaseModel model){
		String username = getUserInSession();
		model.setUsername(username);
		
		modelMap.addAttribute("data", model);
	}
	
	public void applyModel(ModelMap modelMap, AdminBaseModel model, String modelName){
		String username = getUserInSession();
		model.setUsername(username);
		
		modelMap.addAttribute(modelName, model);
	}
}
